﻿#pragma once

#define WIN32_LEAN_AND_MEAN    // 从 Windows 头文件中排除极少使用的内容

#include <stdio.h>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <windows.h>
#include <time.h>
#include "module_xcgui.h"
#include "module_base.h"
using namespace std;

#ifdef  XC_MOUDLE

#endif

typedef unsigned char byte;
typedef byte cs_byte;
typedef unsigned char boolean;

//@模块名称  字节集模块  
//@版本  1.0  
//@日期  2020-10-31
//@作者  情缘  
//@模块备注 模拟易语言字节集功能
// 字节集


//@src "module_zjjl.cpp"

//@隐藏{

static char* wtoa(wchar_t *WStr) {
	//wchar_t *WStr = L"string to convert";

	size_t len = wcslen(WStr) + 1;

	size_t converted = 0;

	char *CStr;

	CStr = (char*)malloc(len * sizeof(char));

	wcstombs_s(&converted, CStr, len, WStr, _TRUNCATE);
	return CStr;
}
static int hexCharToInt(char c)
{
	if (c >= '0' && c <= '9') return (c - '0');
	if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
	if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
	return 0;
}
static char* hexstringToBytes(string s)
{
	int sz = s.length();
	char *ret = new char[sz / 2];
	for (int i = 0; i < sz; i += 2) {
		ret[i / 2] = (char)((hexCharToInt(s.at(i)) << 4) | hexCharToInt(s.at(i + 1)));

	}
	//delete[]ret;
	return ret;
}
static string bytestohexstring(char* bytes, int bytelength)
{
	string str("");
	string str2("0123456789abcdef");
	for (int i = 0; i < bytelength; i++) {
		int b;
		byte kj = bytes[i];
		b = 0x0f & (bytes[i] >> 4);
		char s1 = str2.at(b);
		str.append(1, str2.at(b));
		b = 0x0f & bytes[i];
		str.append(1, str2.at(b));
		char s2 = str2.at(b);
	}
	return str;
}
static void int8ToByte(unsigned char i, byte *bytes, int size = 1)
{
	memset(bytes, 0, sizeof(byte) *  size);
	bytes[0] = (byte)(0xff & i);
	return;
}
static unsigned char bytesToInt8(byte* bytes, int size = 1)
{
	unsigned char addr = bytes[0] & 0xFF;
	return addr;
}
static void  int16ToByte(unsigned short int i, byte *bytes, int size = 2)
{
	memset(bytes, 0, sizeof(byte) *  size);
	bytes[0] = (byte)(0xff & i);
	bytes[1] = (byte)((0xff00 & i) >> 8);
	return;
}
static unsigned short int bytesToInt16(byte* bytes, int size = 2)
{
	unsigned short int addr = bytes[0] & 0xFF;
	addr |= ((bytes[1] << 8) & 0xFF00);
	return addr;
}
static void  intToByte(int i, byte *bytes, int size = 4)
{
	memset(bytes, 0, sizeof(byte) *  size);
	bytes[0] = (byte)(0xff & i);
	bytes[1] = (byte)((0xff00 & i) >> 8);
	bytes[2] = (byte)((0xff0000 & i) >> 16);
	bytes[3] = (byte)((0xff000000 & i) >> 24);
	return;
}

static int bytesToInt(byte* bytes, int size = 4)
{
	int addr = bytes[0] & 0xFF;
	addr |= ((bytes[1] << 8) & 0xFF00);
	addr |= ((bytes[2] << 16) & 0xFF0000);
	addr |= ((bytes[3] << 24) & 0xFF000000);
	return addr;
}

static void Wchar_tToString(std::string& szDst, wchar_t *wchar, DWORD& cd)
{
	wchar_t * wText = wchar;
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);// WideCharToMultiByte的运用
	char *psText; // psText为char*的临时数组，作为赋值给std::string的中间变量
	psText = new char[dwNum];
	cd = dwNum;
	WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);// WideCharToMultiByte的再次运用
	szDst = psText;// std::string赋值
	delete[]psText;// psText的清除
}

static void 文本替换(wchar_t* 内容, wchar_t* 被替换得内容, wchar_t* 要替换得内容) {
};
static   string&   replace_all(string&   str, const  string&  old_value, const  string&  new_value)
{
	while (true)
	{
		string::size_type   pos(0);
		if ((pos = str.find(old_value)) != string::npos)
		{
			str.replace(pos, old_value.length(), new_value);
		}
		else { break; }
	}
	return   str;
}
// str 内容 old_value要替换内容  new_value 被替换内容
static void   子文本替换(string&   str, const  string&  old_value, const   string&   new_value)
{
	for (string::size_type pos(0); pos != string::npos; pos += new_value.length())
	{
		if ((pos = str.find(old_value, pos)) != string::npos)
		{
			str.replace(pos, old_value.length(), new_value);
		}
		else { break; }
	}
	return;// str;
}
//不要忘记使用完char*后delete[]释放内存
static char* wchart_tocahr(wchar_t* pWCStrKey, int& cd)
{
	//第一次调用确认转换后单字节字符串的长度，用于开辟空间
	int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);
	char* pCStrKey = new char[pSize + 1];
	cd = pSize;
	//第二次调用将双字节字符串转换成单字节字符串
	WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);
	pCStrKey[pSize] = '\0';
	return pCStrKey;

	//如果想要转换成string，直接赋值即可
	//string pKey = pCStrKey;
}
//不要忘记使用完char*后delete[]释放内存
static char* wideCharToMultiByte(wchar_t* pWCStrKey)
{
	//第一次调用确认转换后单字节字符串的长度，用于开辟空间
	int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);
	char* pCStrKey = new char[pSize + 1];
	//第二次调用将双字节字符串转换成单字节字符串
	WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);
	pCStrKey[pSize] = '\0';
	return pCStrKey;

	//如果想要转换成string，直接赋值即可
	//string pKey = pCStrKey;
}
// wchar_t to string
static void Wchar_tToString(std::string& szDst, wchar_t*wchar)
{
	wchar_t * wText = wchar;
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);//WideCharToMultiByte的运用
	char *psText;  // psText为char*的临时数组，作为赋值给std::string的中间变量
	psText = new char[dwNum];
	WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);//WideCharToMultiByte的再次运用
	szDst = psText;// std::string赋值
	delete[]psText;// psText的清除
}

// string to wstring
static void StringToWstring(std::wstring& szDst, std::string str)
{
	std::string temp = str;
	int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
	wchar_t * wszUtf8 = new wchar_t[len + 1];
	memset(wszUtf8, 0, len * 2 + 2);
	MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len);
	szDst = wszUtf8;
	std::wstring r = wszUtf8;
	delete[] wszUtf8;
}

//@隐藏}

//@备注 字节集模块
//@别名 字节集类
class zjjl
{
public:
	zjjl() { 
		sjcd = 0;
		sj = NULL;
		_bMove = false;
	}
	zjjl(int cd) {
		sjcd = cd;
		unsigned char* bytes3 = new unsigned char[sjcd + 1];
		memset(bytes3, 0, sjcd); // right
		sj = bytes3;
		_bMove = false;
	};
	zjjl(const zjjl& _zjjl) {
		sj = _zjjl.sj;
		if (_zjjl._bMove) //数据所有权已被移走, 此处只是数据的副本, 所以复制数据
		{
			//_text = new std::wstring;
			*sj = *_zjjl.sj;
			//*sjcd = *_zjjl.sjcd;
			//*_bMove = *_zjjl._bMove;

			//sj = new unsigned char[sjcd + 1];
			//memcpy(sj, pt.sj, sjcd);
		} else //获得数据所有权,避免不必要的数据复制
		{
			//sj = _zjjl.sj;
			sjcd = _zjjl.sjcd;
			((zjjl&)_zjjl).move();

		}

	
	}
	
	//@别名 添加数据()
	void Append(void* a, int cd) {
		//return;
		if (sjcd == 0) {
			sjcd = cd;
			unsigned char* bytes3 = new unsigned char[sjcd + 1];
			memcpy(bytes3, a, cd);
			sj = bytes3;
		}
		else {


			unsigned char* bytes3 = new unsigned char[sjcd + cd + 1];
			memcpy((byte *)memcpy(bytes3, sj, sjcd) + sjcd, a, cd);
			sjcd += cd;
			delete[]sj;
			sj = bytes3;
		}
	};
	zjjl(void* a, int cd) {
		//if (sjcd == 0) {
			//sjcd = cd;
			//unsigned char* bytes3 = new unsigned char[sjcd + 1];
			//memcpy(bytes3, a, cd);
			//sj = bytes3;
			//_bMove = false;
		//}
		//else {
			sjcd = cd;
			unsigned char* bytes3 = new unsigned char[sjcd + 1];
			memcpy(bytes3, a, cd);
			//delete[]sj;
			sj = bytes3;
			//unsigned char* bytes3 = new unsigned char[sjcd + a.sjcd + 1];
			//memcpy((byte *)memcpy(bytes3, sj, sjcd) + sjcd, a.sj, a.sjcd);
			//sjcd += a.sjcd;
			//delete[]sj;
			//sj = bytes3;
		//}
	};
	//@别名 添加字节集()
	void Append(zjjl a) {
		if (sjcd == 0) {
			sjcd = a.sjcd;
			unsigned char* bytes3 = new unsigned char[sjcd + 1];
			memcpy(bytes3, a.sj, a.sjcd);
			sj = bytes3;
		}
		else {


			unsigned char* bytes3 = new unsigned char[sjcd + a.sjcd + 1];
			memcpy((byte *)memcpy(bytes3, sj, sjcd) + sjcd, a.sj, a.sjcd);
			sjcd += a.sjcd;
			delete[]sj;
			sj = bytes3;
		}
	};
	//CXText(const wstring &wstring_) { _text = new std::wstring;  *_text = wstring_; }
	//CXText(const wchar_t* pText) { _text = new std::wstring; if (pText) *_text = pText; }
	~zjjl();
	

	zjjl &operator=(const zjjl &cp)
	{
		//cout << "赋值重载函数 " << this << " " << endl;
		if (this != &cp)
		{
			if (cp.sjcd == 0) {
				if (sjcd > 0) {
					sjcd = 0;
					delete[]sj;
				}

			}else{
				if (sjcd > 0) {
					sjcd = 0;
					delete[]sj;
				}

				sjcd = cp.sjcd;
				sj = new unsigned char[sjcd + 1];
				memcpy(sj, cp.sj, cp.sjcd);
				//sj = bytes3;
			}
		}
		return (*this);
	}


	//void operator=(const wchar_t* pText) { *_text = pText ? pText : L""; }
	//void operator=(const wstring &wstring_) { *_text = wstring_; }
	//bool operator==(const wchar_t* pText) const { return (*_text == pText); }
	//bool operator<(const CXText &_right) const { return (_text->compare(*_right._text) < 0); }
	//operator const wchar_t*() const {return(_text->c_str()); }//当作为函数参数传递时, 自动转换为[const char*]


	//@别名  取地址地址()
	const byte*  tobyte();
	//@别名 取字节集长度()
	int  size();
	//@别名 取字节集长度()
	const zjjl 取字节集左边(int 长度) {
		zjjl k(sj, 长度);
		return k;
	}
	//@别名 取字节集右边()
	const zjjl 取字节集右边(int 长度) {
		int cd = sjcd;
		cd = cd - 长度;
		zjjl k(sj + cd, 长度);
		return k;
	}
	//@别名 取字节集中间()
	const zjjl 取字节集中间(int 开始位置, int 长度) {
		zjjl k(sj + 开始位置, 长度);
		return k;
	}
	//@别名 取空白字节集()
	const zjjl 取空白字节集(int 长度) {
		zjjl k(长度);
		return k;
	}
	void 添加文本_(const wchar_t* 内容) {
		char    IP_pz[256] = { '\0' };
		int cd = 0;
		cd = wcslen(内容);
		XC_UnicodeToAnsi(内容, cd, IP_pz, 256);
		zjjl k(IP_pz, cd);
		Append(k);
		return;
	}
	void 添加十六进制(wchar_t* 内容) {
		DWORD cd;
		string s;
		//string ss;
		Wchar_tToString(s, 内容, cd);
		//s = 内容;
		//ss = XC_wtoa(内容);
		cd -= 1;
		char* pp = (hexstringToBytes(s));

		zjjl cs(pp, (cd / 2) * sizeof(CHAR));
		delete[] pp;
		Append(cs);
		return;
	}
	void 添加文本(const wchar_t* 内容) {
		DWORD cd;
		string s;
		//string ss;
		Wchar_tToString(s, (wchar_t*)内容, cd);
		//s = 内容;
		//ss = XC_wtoa(内容);
		cd -= 1;
		zjjl cs((void*)s.c_str(), (cd) * sizeof(CHAR));
		Append((void*)(s.c_str()), (cd) * sizeof(CHAR));
		return ;
	}
	//void 添加文本(CXText nr) {
		//xc_log(L"开始增加文本");
		//Append((void*)(nr.toStr()), nr.size());
		//return;
	//}
	const wchar_t* 到文本(int 位置=0,int 长度=0) {
		if (位置<=0){
			//return *(const wchar_t*)(sj);
			位置 = 0;
		}
		if (长度<=0){
			//return *(const wchar_t*)(sj+ 位置);
			长度 = sjcd;
		}
		zjjl wbsj = 取字节集中间(位置, 长度);
		wbsj.Append("\0", 1);
		return XC_atow((const char*)wbsj.tobyte());
		//return *(const wchar_t*)(wbsj.tobyte());
	}
	//@别名 到十六进制()
	const wchar_t* 到十六进制() {
		//string str("");
		//str.append(bytestohexstring(a.hqsj(), a.qzjjcd()));
		string kk = bytestohexstring((char*)sj, sjcd);
		//rg_n4786 = CVolString(kk.c_str());
		//int len = kk.length;
		//char *cp = new char[len + 1];
		//StrPCopy(cp, str);
		return XC_atow(kk.c_str());
	}
	int 到整数(int 位置=0) {
		//int zs = bytesToInt(sj+ 位置);
		//*(TCHAR*)(m_pData + npOffset)
		if (位置<0){
			位置=0;
		}
		return *(INT*)(sj + 位置);//zs;
	}
	void 添加整数(int a) {
		Append((void*)(&a),4);
		return ;
	}

	short 到短整数(int 位置=0) {
		if (位置<0){
			位置=0;
		}
		return *(short*)(sj + 位置);
	}
	double 到浮点数(int 位置=0) {
		if (位置<0){
			位置=0;
		}
		return *(double*)(sj + 位置);
	}
	void 添加短整数(SHORT a) {
		Append((void*)(&a),2);
		return;
	}
	void 添加字节(byte a) {
		Append((void*)(&a),1);
		return;
	}
	void 添加浮点数(double a) {
		Append((void*)(&a),8);
		return;
	}
	byte 到字节(int 位置) {
		if (位置<0){
			位置=0;
		}
		//byte k = bytesToInt8(sj+ 位置);
		//return k;
		return *(byte*)(sj + 位置);
	}

private:
	//move()移出数据所有权, 当所有权被移除后,析构时保留数据, 
	//如果 _bMove=TRUE 数据所有权已被移走, 析构时不需要销毁数据
	//如果 _bMove=FALSE 持有数据所有权, 析构时销毁数据
	//与智能指针不同, 没有引用计数, 只有持有所有权的对象销毁时 才销毁数据,其他对象只是对数据的引用
	//当对象赋值时, 自动调用此方法, 移交所有权
	//例如:
	//CXText textA = "123";
	//CXText textB = textA;  //自动调用move()函数移动所有权, 如果textA没有所有权,只是一个副本, 那么仅复制数据
	//此方法的作用减少内存的复制, 当 textA 赋值给 textB时, 仅是把数据指针赋值给B,并不会复制数据, 减少内存复制带来的开销
	void move() { _bMove = true;}
	int sjcd = 0;
	byte* sj = NULL;
	BOOL _bMove=false;
};
